package net.huashitong.ssydt.provider.comment.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huashitong.ssydt.provider.annotations.LoginRequired;
import net.huashitong.ssydt.provider.comment.service.CommentService;
import net.huashitong.ssydt.provider.comment.vo.*;
import net.huashitong.ssydt.provider.vo.UserBaseLongIdPagingVO;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import net.huashitong.ssydt.security.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.09.20
 */
@Api(value = "/api/comment", tags = {"评论模块"})
@RestController
@RequestMapping("/api/comment")
public class ApiCommentController {

    private CommentService commentService;

    @Autowired
    public ApiCommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @ApiOperation(value = "评论详情", notes = "评论详情", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @GetMapping("/comments/{commentId}")
    public Result<MyCommentVO> getComments(
            @ApiParam(value = "评论id", required = true) @PathVariable("commentId") Long commentId) {
        String userId = UserUtils.getUserId();
        MyCommentVO commentVO = commentService.getComments(userId, commentId);
        return ResultUtils.getSuccessResults(commentVO);
    }


    @ApiOperation(value = "回复评论", notes = "回复评论", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping("/comments/{commentId}")
    public Result<String> postReplyComments(
            @ApiParam(value = "评论id", required = true) @PathVariable("commentId") Long commentId,
            @RequestBody ReplyCommentVO replyCommentVO) {
        String userId = UserUtils.getUserId();
        replyCommentVO.setUserId(userId);
        replyCommentVO.setCommentId(commentId);
        commentService.postReplyComments(replyCommentVO);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "删除评论", notes = "删除评论", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @DeleteMapping("/comments/{commentId}")
    public Result<String> deleteComments(
            @ApiParam(value = "评论id", required = true) @PathVariable("commentId") Long commentId) {
        String userId = UserUtils.getUserId();
        commentService.deleteComments(commentId, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "评论点赞", notes = "评论点赞", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping("/comments/{commentId}/praises")
    public Result<String> postCommentPraises(
            @ApiParam(value = "评论id", required = true) @PathVariable("commentId") Long commentId) {
        String userId = UserUtils.getUserId();
        commentService.postCommentPraises(commentId, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "获取评论列表", notes = "获取评论列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/comments")
    public Result<ArrayList<CommentVO>> getCommentList(CommentListQueryVO commentListQueryVO) {
        String userId = UserUtils.getUserId();
        commentListQueryVO.setUserId(userId);
        List<CommentVO> commentVOS = commentService.getCommentList(commentListQueryVO);
        return ResultUtils.getSuccessResultList(commentVOS);
    }


    @ApiOperation(value = "发表评论", notes = "发表评论", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping("/comments")
    public Result<String> postComments(@RequestBody SsydtCommentInVo ssydtCommentInVo) {
        String userId = UserUtils.getUserId();
        ssydtCommentInVo.setUserId(userId);
        commentService.postComments(ssydtCommentInVo);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "取消评论点赞", notes = "取消评论点赞", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @DeleteMapping("/comments/{commentId}/praises")
    public Result<String> deleteCommentPraises(
            @ApiParam(value = "评论id", required = true) @PathVariable("commentId") Long commentId) {
        String userId = UserUtils.getUserId();
        commentService.deleteCommentPraises(commentId, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "我的评论列表", notes = "我的评论列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/comments/my")
    public Result<ArrayList<MyCommentVO>> getMyCommentList(UserBaseLongIdPagingVO paging) {
        String userId = UserUtils.getUserId();
        paging.setUserId(userId);
        List<MyCommentVO> myCommentVOS = commentService.getMyCommentList(paging);
        return ResultUtils.getSuccessResultList(myCommentVOS);
    }

}
